<?php

namespace App\Store\Models\OldMaterial;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use Illuminate\Database\Eloquent\Model;

class Allot extends Model
{
    protected $table = 'site_oldmaterial_allot';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList($request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $store_ids = $accountinfo['store_ids'];

        $query = self::with(['startstoreinfo', 'targetstoreinfo', 'infolist' => function($query) {
            $query->where('is_del', 1);
        }]);

        if (!empty($store_ids)) {
            if(is_array($store_ids)){
                $query->where(function($q) use ($store_ids) {
                    $q->whereIn('start_store_id', $store_ids)
                        ->orWhereIn('target_store_id', $store_ids);
                });
            }else{
                $query->where(function($q) use ($store_ids) {
                    $q->where('start_store_id', $store_ids)
                        ->orWhere('target_store_id', $store_ids);
                });
            }
        }

        // 创建时间
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        //目标门店
        $start_store_id = $request['start_store_id'] ?? 0;
        if ($start_store_id > 0) {
            $query->where('start_store_id', $start_store_id);
        }
        $target_store_id = $request['target_store_id'] ?? 0;
        if ($target_store_id > 0) {
            $query->where('target_store_id', $target_store_id);
        }


        $ids = $request->ids;
        $ids = is_array($ids)?$ids:(is_array(json_decode($ids,true))?json_decode($ids,true):[]);
        if(count($ids)>0){
            $query->whereIn('id', $ids);
        }
        //订单状态
        $audit_status = $request['audit_status'] ?? 0;
        if ($audit_status > 0) {
            $query->where('audit_status', $audit_status);
        }

        // 调拨单号
        $order_sncode = $request['order_sncode'] ?? '';
        if ($order_sncode != '') {
            $query->where('order_sncode', 'like', '%' . $order_sncode . '%');
        }

        $query->where('is_del', 1)->where('cp_id', $cp_id)->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    /**
     * 外联明细表
     */
    public function infolist()
    {
        return $this->hasMany(AllotInfolist::class, 'p_id', 'id');
    }

    /**
     * 店铺
     */
    public function startstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'start_store_id')->select('id', 'store_name');
    }

    public function targetstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'target_store_id')->select('id', 'store_name');
    }
}

